package mao.thinkinjava.io;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class GetChannel {
    public static void main(String[] args) throws IOException {
        FileChannel fc = new FileOutputStream("data.txt").getChannel();
        fc.write(ByteBuffer.wrap("some text".getBytes()));
        fc.close();

        fc = new RandomAccessFile("data.txt","rw").getChannel();
        fc.position(fc.size());
        fc.write(ByteBuffer.wrap(" more text".getBytes()));
        fc.close();

        fc = new FileInputStream("data.txt").getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //ByteBuffer.allocateDirect();
        fc.read(buffer);
        fc.close();
        //使得buffer的指针回到开始的地方。
        buffer.flip();

        while (buffer.hasRemaining()){
            System.out.print((char)buffer.get());
        }


    }
}
